package com.toKnow.controller;


import com.toKnow.model.entity.*;
import com.toKnow.model.vo.ThreeReturn;
import com.toKnow.service.ColumnPubService;
import com.toKnow.service.TypeTagPubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/api")
public class ColumnPubController {

    @Autowired
    private ColumnPubService columnPubService;
    @Autowired
    private TypeTagPubService typeTagPubService;

    public void setColumnPubService(ColumnPubService columnPubService) {
        this.columnPubService = columnPubService;
    }

    @RequestMapping(value = "/column/content/submit",method = RequestMethod.POST)
    @ResponseBody
    public ThreeReturn addColumn(Integer user_id, String title, Integer date, String type, String tag){

        //若前端不提供 user_id 则 从token 中获取
        //String userToken = (String) request.getSession().getAttribute("userToken");
        //Integer user_id = Integer.valueOf( JwtUserSessionUtil.getInfo(userToken,"id") );
/*

        System.out.println(user_id);
        System.out.println(title);
        System.out.println("type = "+type);
*/

        List<String> type1 =null;
        List<String> tag1 = null;

        if(tag!=null ){

            tag1 = Arrays.asList(tag.split(","));
        }
        if(type!=null){
            type1 = Arrays.asList(type.split(","));
        }



        //插入专栏信息
        ColumnPub columnPub = new ColumnPub();
        columnPub.setAuthor_id(user_id);
        columnPub.setTitle(title);
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        columnPub.setPublish_date(ts);
        //修改用户发表专栏数
        UserInfoPub userInfoPub =new UserInfoPub();
        userInfoPub.setId(user_id);
        Integer column_id= columnPubService.addColumnReturnId(columnPub,userInfoPub);

        //将type tag名 改为id
        List<Integer> TypeIds = typeTagPubService.convertTypeNameToId(type1);
        List<Integer> TagIds = typeTagPubService.convertTagNameToId(tag1);

        //System.out.println("TypeIds= "+TypeIds);

        //为专栏添加 type tag
        if(TypeIds!=null){
            for(Integer id : TypeIds){
                ColumnTypePub columnTypePub = new ColumnTypePub();
                columnTypePub.setColumn_id(column_id);
                columnTypePub.setType_id(id);
                columnPubService.addType(columnTypePub);
            }
        }

        if(TagIds!=null){
            for (Integer id:TagIds){
                ColumnTagPub columnTagPub = new ColumnTagPub();
                columnTagPub.setColumn_id(column_id);
                columnTagPub.setTag_id(id);
                columnPubService.addTag(columnTagPub);
            }
        }

        ThreeReturn threeReturn = new ThreeReturn();


        if( column_id != null ){
            threeReturn.setSuccess(true);
            threeReturn.setMessage("success");
        }else {
            threeReturn.setSuccess(false);
            threeReturn.setMessage("false");
        }
        return threeReturn;

    }

    public static void main(String[] args) {
        String str = null;
        List<String> res = Arrays.asList(str.split(","));
        System.out.println(res);

    }


}
